import { useUserinfo } from "@/stores/userInfo";

export const permission = {
    mounted(el: HTMLElement, binding: { value?: string[] }) { // 给 value 加可选符
        const { scope } = useUserinfo();
        // 1. 先校验 binding.value 类型，不是数组则转为空数组
        const permissionList = Array.isArray(binding.value) ? binding.value : [];
        // 2. 再判断是否有权限，无权限则移除元素
        if (!permissionList.find(item => scope.includes(item))) {
            el.parentNode?.removeChild(el);
        }
    }
}

export const role = (el: HTMLElement, binding: { value: string[] }) => {
    const {roles} =useUserinfo();

    if(!binding.value && roles.includes(binding.value)) {
        el.style.display='none'
    }
}

export const autherization={
    install(app:Record<'directive',(p:string,p2:unknown)=>void>) {
        app.directive('permission', permission)
        app.directive('role', role)
    }
}